package com.ywl.gulimall.product.service.impl;

import com.ywl.common.constants.ProductConstant;
import com.ywl.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.ywl.gulimall.product.entity.AttrEntity;
import com.ywl.gulimall.product.service.AttrAttrgroupRelationService;
import com.ywl.gulimall.product.service.AttrService;
import com.ywl.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywl.common.utils.PageUtils;
import com.ywl.common.utils.Query;

import com.ywl.gulimall.product.dao.AttrGroupDao;
import com.ywl.gulimall.product.entity.AttrGroupEntity;
import com.ywl.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        Object key = params.get("key");
        if (key != null) {
            wrapper.and(obj -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }

        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        } else {
            // SELECT * FROM pms_attr_group t WHERE t.catelog_id=1 AND (t.attr_group_id = 1 OR t.attr_group_name LIKE '%1%')
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrEntity> getAttrRelation(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> attrgroupRelationEntities = attrAttrgroupRelationService
                .list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
        List<Long> list = attrgroupRelationEntities.stream().map((item) -> {
            return item.getAttrId();
        }).collect(Collectors.toList());
        List<AttrEntity> entities = new ArrayList<>();
        if (list.size() > 0) {
            entities = attrService.listByIds(list);
        }
        return entities;
    }

    @Override
    public void deleteAttrRelation(List<AttrAttrgroupRelationEntity> relationEntities) {
        attrAttrgroupRelationService.deleteAttrRelation(relationEntities);
    }

    @Override
    public PageUtils getAttrNoRelation(Map<String, Object> params, Long attrGroupId) {
        // 1、获取当前分类id
        AttrGroupEntity attrGroup = this.getById(attrGroupId);
        // 2、获取当前分类下所有分组id
        List<AttrGroupEntity> groupEntities = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>()
                .eq("catelog_id", attrGroup.getCatelogId()));
        List<Long> longList = groupEntities.stream().map(item -> {
            return item.getAttrGroupId();
        }).collect(Collectors.toList());
        // 3、获取当前分类下所有分组id关联的属性id
        List<Long> attrIds = new ArrayList<>();
        if (longList != null && longList.size() > 0) {
            List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationService
                    .list(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", longList));
            attrIds = relationEntities.stream().map(item -> {
                return item.getAttrId();
            }).collect(Collectors.toList());
        }
        // 4、获取当前分类下所有属性id并排除上面已经关联的id
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>()
                .eq("catelog_id", attrGroup.getCatelogId())
                .eq("attr_type", ProductConstant.ATTR_STYLE_BASE.getCode());

        if (attrIds.size() > 0) {
            wrapper.notIn("attr_id", attrIds);
        }

        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            wrapper.and(w -> {
                w.eq("attr_id", key).or().like("attr_name", key);
            });
        }

        IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Override
    public void saveAttrRelation(List<AttrAttrgroupRelationEntity> relationEntities) {
        attrAttrgroupRelationService.saveBatch(relationEntities);
    }

    @Override
    public List<AttrGroupWithAttrsVo> listWithAttrs(Long catelogId) {
        // 1、查询分类下所有分组
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        // 2、查询分组下所有属性
        List<AttrGroupWithAttrsVo> collect = groupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo vos = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, vos);
            List<AttrEntity> relation = getAttrRelation(group.getAttrGroupId());
            vos.setAttrs(relation);
            return vos;
        }).collect(Collectors.toList());
        return collect;
    }

}